package com.lindroy.androidplayer.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lindroy.androidplayer.logic.network.repository.Repository
import com.lindroy.androidplayer.ui.widget.SettingBar
import com.lindroy.androidplayer.utils.SingleLiveEvent
import com.lindroy.androidplayer.utils.mapSingle
import com.lindroy.androidplayer.utils.switchMapSingle
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

/**
 * @author Lin
 * @date 2022/2/28
 * @function
 */
class SettingsViewModel : ViewModel() {

    private val _bannerCheckedState = SingleLiveEvent<Boolean>()

    private val _topArticleCheckedState = SingleLiveEvent<Boolean>()

    private val _bannerCheckedFlow = MutableSharedFlow<Boolean>()

    private val _topArticleFlow = MutableSharedFlow<Boolean>()

    private val _logoutConfirmLiveData = SingleLiveEvent<Unit>()

    private val logoutClickAction = SingleLiveEvent<Unit>()

    private val privateClickAction = SingleLiveEvent<Unit>()

    private val _cacheSizeLiveData = SingleLiveEvent<String>()

    private val clearCacheClickAction = SingleLiveEvent<Unit>()

    private val aboutUsClickAction = SingleLiveEvent<Unit>()

    val logoutConfirmLiveData = _logoutConfirmLiveData.switchMapSingle { Repository.logout() }

    val bannerCheckedState = _bannerCheckedState.mapSingle { it }

    val bannerCheckedFlow = _bannerCheckedFlow.map { it }.distinctUntilChanged()

    val topArticleFlow = _topArticleFlow.map { it }.distinctUntilChanged()

    val logoutClickLiveData = logoutClickAction.mapSingle {}

    val topArticleCheckState = _topArticleCheckedState.mapSingle { it }

    val privateLiveData = privateClickAction.mapSingle { it }

    val cacheSizeLiveData = _cacheSizeLiveData.mapSingle { it.orEmpty() }

    val clearCacheLiveData = clearCacheClickAction.mapSingle { it }

    val aboutUsLiveData = aboutUsClickAction.mapSingle { it }

    val privateItemClickListener = SettingBar.OnItemClickListener {
        privateClickAction.postValue(Unit)
    }

    val clearCacheItemClickListener = SettingBar.OnItemClickListener {
        clearCacheClickAction.postValue(Unit)
    }

    val showBannerCheckedListener = SettingBar.OnCheckedChangeListener {
        viewModelScope.launch {
            _bannerCheckedFlow.emit(it)
        }
    }

    val showTopArticleListener = SettingBar.OnCheckedChangeListener {
        viewModelScope.launch {
            _topArticleFlow.emit(it)
        }
    }

    val aboutUsClickListener = SettingBar.OnItemClickListener {
        aboutUsClickAction.postValue(Unit)
    }

    fun setBannerShowState(isShow: Boolean) {
        _bannerCheckedState.postValue(isShow)
    }

    fun setTopArticleShowState(isShow: Boolean) {
        _topArticleCheckedState.postValue(isShow)
    }

    fun onLogoutClick() {
        logoutClickAction.postValue(Unit)
    }

    fun logout() {
        _logoutConfirmLiveData.postValue(Unit)
    }

    fun setCacheSize(cacheSize: String) {
        _cacheSizeLiveData.postValue(cacheSize)
    }

}